package com.aeye.mbr.upms.rpc.service.impl;

import com.aeye.mbr.common.plugin.mapper.BaseServiceImpl;
import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.result.BootstrapTableResult;
import com.aeye.mbr.upms.dao.model.UpmsSystem;
import com.aeye.mbr.upms.dao.model.UpmsTerminal;
import com.aeye.mbr.upms.rpc.api.UpmsSystemService;
import com.aeye.mbr.upms.rpc.api.UpmsTerminalService;
import com.aeye.mbr.upms.rpc.mapper.UpmsSystemMapper;
import com.aeye.mbr.upms.rpc.mapper.UpmsTerminalMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * UpmsSystemService实现
 * Created by shuaeye.mbr on 2017/3/20.
 */
@Service
@Transactional
public class UpmsTerminalServiceImpl extends BaseServiceImpl<UpmsTerminal> implements UpmsTerminalService {

    private static Logger _log = LoggerFactory.getLogger(UpmsTerminalServiceImpl.class);

    @Autowired
    UpmsTerminalMapper upmsTerminalMapper;

    @Override
    public BootstrapTableResult<UpmsTerminal> selectUpmsTerminalListByParams(BootstrapTableParams params) {
        BootstrapTableResult result = new BootstrapTableResult();
        if (params != null) {
            PageHelper.startPage(params.getPage(), params.getLimit());
            Example example = new Example(UpmsSystem.class);
            if (!StringUtils.isBlank(params.getSort()) && !StringUtils.isBlank(params.getOrder())) {
                example.setOrderByClause(params.getSort() + " " + params.getOrder());
            }
            if (StringUtils.isNotBlank(params.getSearch())) {
                example.or()
                        .andLike("name", "%" + params.getSearch() + "%");
            }
            List<UpmsTerminal> systems = upmsTerminalMapper.selectByExample(example);
            if(!CollectionUtils.isEmpty(systems)){
                if(systems instanceof Page){
                    Page<UpmsTerminal> pls = (Page)systems;
                    result.setTotal((int)pls.getTotal());
                    result.setRows(pls.getResult());
                }else{
                    result.setTotal(systems.size());
                    result.setRows(systems);
                }
            }
        }
        return result;
    }

}